import { AppState, DeviceEventEmitter, NativeAppEventEmitter } from 'react-native';
const JPushModule = require('jpush-react-native').default;
const { Toast } = require('antd-mobile');
const { Actions } = require('react-native-router-flux');
import { urlReg } from './utils/Reg';
let shareInstance = null;
export class NoticeEvent {
    constructor(type, text) {
        this.text = text;
        this.type = type;
    }
}
export default class PushHandle {
    constructor(props) {
        shareInstance = this;
        this._handleAppStateChange = this._handleAppStateChange.bind(this);
        this.appState = AppState.currentState;
    }
    static init(props) {
        this.setBadge(0);
        AppState.addEventListener('change', this.shareInstance(props)._handleAppStateChange);
        JPushModule.addOpenNotificationLaunchAppListener((e) => {
            console.log('应用没有启动的状态点击推送打开应用', e);
            this.postNotice(new NoticeEvent('newOrder', '收到新订单'));
        });
        JPushModule.addnetworkDidLoginListener(() => {
            console.log('应用连接已登录');
        });
        JPushModule.addReceiveNotificationListener((e) => {
            JPushModule.getBadge((number) => {
                this.setBadge(number + 1);
            });
            this.setBadge(0);
            this.postNotice(new NoticeEvent('newOrder', '收到新订单'));
        });
        JPushModule.addReceiveOpenNotificationListener((e) => {
            if (urlReg.test(e.data)) {
                Actions.web({ source: { uri: e.data } });
            }
            else {
                this.postNotice(new NoticeEvent('newOrder', '收到新订单'));
            }
            this.setBadge(0);
        });
        JPushModule.addReceiveExtrasListener((e) => {
            console.log('收到 Native 下发的 extra 事件', e);
        });
    }
    static shareInstance(props) {
        if (!shareInstance) {
            shareInstance = new this(props);
        }
        return shareInstance;
    }
    static setBadge(number) {
        JPushModule.setBadge(number, (success) => {
            if (success) {
                console.log('清除徽标成功', success);
            }
        });
    }
    static setAlias(alias) {
        JPushModule.setAlias(alias + '', (res) => {
            console.log('设置别名成功!!!!', res, alias);
        });
    }
    static destroy() {
        AppState.removeEventListener('change', this.shareInstance()._handleAppStateChange);
        DeviceEventEmitter.removeAllListeners();
        NativeAppEventEmitter.removeAllListeners();
    }
    _handleAppStateChange(state) {
        console.log('应用状态更改', state);
        this.appState = state;
    }
    static postNotice(notice) {
        console.log('发送通知');
        DeviceEventEmitter.emit('receiveNewOrder');
    }
}
;
